package com.sugon.ww

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}


object WordCount {

  def main(args: Array[String]): Unit = {


    val conf: SparkConf = new SparkConf().setMaster("local").setAppName("wordCount")

    val sc = new SparkContext(conf)

    val lines: RDD[String] = sc.textFile("in/word.txt")

    val words: RDD[String] = lines.flatMap(_.split(" "))

    val wordToOne: RDD[(String, Int)] = words.map((_, 1))

    val wordToSum: RDD[(String, Int)] = wordToOne.reduceByKey(_ + _)

    val result: Array[(String, Int)] = wordToSum.collect()

    result.foreach(println)
  }
}
